package com.java.base.exercise;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;

/**
 * @Author RenPu
 * @Date 2021/9/29 14:46
 * @Version 1.0
 * @Description:
 **/
public class FutureTaskDemo {

    public static void main(String[] args) throws Exception {

        NewTask newTask = new NewTask("123132");
        FutureTask futureTask = new FutureTask<String>(newTask);
        new Thread(futureTask).start();
        System.out.println(futureTask.get());


        FutureTask futureTask1 = new FutureTask<Integer>(new MyRunable(), 22);
        new Thread(futureTask).start();
        System.out.println(futureTask1.get());


    }


    static class NewTask implements Callable<String> {
        private String taskName;

        public NewTask(String taskName) {
            this.taskName = taskName;
        }

        public NewTask() {
        }


        @Override
        public String call() throws Exception {

            TimeUnit.SECONDS.sleep(2);

            System.out.println("任务计算完成 ！ ！ ！ !");
            return "result_1";
        }
    }


    static class MyRunable implements Runnable {

        @Override
        public void run() {

            try {
                TimeUnit.SECONDS.sleep(2);

                System.out.println("特定线程，计算成功 ！ ！ ！");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
    }

}

